package com.project.daojishi;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.content.Intent;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SecondFragment extends Fragment {

    ImageView add;
    ListView rlHome;
    DiaryAdapter diaryAdapter;
    ImageView setting;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second, container, false);
        rlHome=view.findViewById(R.id.rl_home);
        add=view.findViewById(R.id.add);
        setting=view.findViewById(R.id.setting);
        // Inflate the layout for this fragment

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(getActivity(), AddActivity.class), 101);
            }
        });
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),SettingActivity.class));
            }
        });
        diaryAdapter = new SecondFragment.DiaryAdapter(getActivity(), SqliteDBUtils.getInstance(getActivity()).load());
        rlHome.setAdapter(diaryAdapter);
        return view;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        diaryAdapter = new SecondFragment.DiaryAdapter(getActivity(), SqliteDBUtils.getInstance(getActivity()).load());
        rlHome.setAdapter(diaryAdapter);
    }

    class DiaryAdapter extends BaseAdapter {
        private Context context;
        private List<Diary> diaryList;

        public DiaryAdapter(Context context, List<Diary> diaryList) {
            this.context = context;
            this.diaryList = diaryList;
        }

        @Override
        public int getCount() {
            return diaryList.size();
        }

        @Override
        public Object getItem(int i) {
            return diaryList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            SecondFragment.DiaryAdapter.ViewHolder holder = null;
            if (view == null) {
                holder = new SecondFragment.DiaryAdapter.ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.item, null);
                holder.ll = view.findViewById(R.id.ll);
                holder.tv_time = view.findViewById(R.id.tv_time);
                holder.tv_title = view.findViewById(R.id.tv_title);
                holder.tv_content = view.findViewById(R.id.tv_content);
                view.setTag(holder);
            } else {
                holder = (SecondFragment.DiaryAdapter.ViewHolder) view.getTag();
            }
            holder.tv_time.setText(diaryList.get(i).getTime());
            holder.tv_title.setText(diaryList.get(i).getTitle());
            holder.tv_content.setText(content(Utils.getTime(), diaryList.get(i).getTime()));
            holder.ll.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    SqliteDBUtils.getInstance(context.getApplicationContext()).delete(getActivity(), diaryList.get(i).getId());
                    Toast.makeText(getActivity(),"删除成功",Toast.LENGTH_SHORT).show();
                    diaryAdapter = new DiaryAdapter(getActivity(), SqliteDBUtils.getInstance(getActivity()).load());
                    rlHome.setAdapter(diaryAdapter);
                    return true;
                }
            });
            return view;
        }

        class ViewHolder {
            LinearLayout ll;
            TextView tv_time, tv_title, tv_content;
        }
    }

    private String content(String start, String end) {
        long day = getDay(start, end);
        if (day > 0) {
            return "已经过了" + Math.abs(day) + "天";
        } else if (day == 0) {
            return "就在今天";
        } else {

            return "还有" + Math.abs(day) + "天";
        }

    }

    private long getDay(String start, String end) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

            Date startTime = df.parse(start);
            Date endTime = df.parse(end);

            long diff = startTime.getTime() - endTime.getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            return days;
        } catch (Exception e) {
            return 0;
        }
    }



}